package com.punai.dataanalysis.epdatakb.service.NG001;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.punai.common.core.domain.BaseEntity;
import com.punai.common.core.domain.entity.EcoCompany;
import com.punai.common.exception.ServiceException;
import com.punai.common.utils.NumberUtil;
import com.punai.dataanalysis.epdatakb.bo.JtczResultBo;
import com.punai.dataanalysis.epdatakb.bo.NG001.JtczQfycbzb;
import com.punai.dataanalysis.epdatakb.bo.NG001.JtczRcf;
import com.punai.dataanalysis.epdatakb.mapper.JtczTjRgfMapper;
import com.punai.dataanalysis.epdatakb.vo.DataAnalysisVo;
import com.punai.system.mapper.EcoCompanyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class NG001JtczRcfService {

    @Autowired
    private JtczTjRgfMapper tjRgfMapper;
    @Autowired
    private EcoCompanyMapper ecoCompanyMapper;
    public List<? extends BaseEntity> getDataList(DataAnalysisVo dataAnalysisVo) {

        Map<String, Object> yearMonth = dataAnalysisVo.getYearMonth();

        List<JtczRcf> resultList = new ArrayList<>();
        List<JtczResultBo> tjwccz = tjRgfMapper.tjwccz(yearMonth);
        Map<String, BigDecimal> tjwcczMap = tjwccz.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal));
        List<JtczResultBo> tjrgf = tjRgfMapper.tjrgf(yearMonth);
        Map<String, BigDecimal> tjrgfMap = tjrgf.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal));
        List<JtczResultBo> azwccz = tjRgfMapper.azwccz(yearMonth);
        Map<String, BigDecimal> azwcczMap = azwccz.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal));
        List<JtczResultBo> azrgf = tjRgfMapper.azrgf(yearMonth);
        Map<String, BigDecimal> azrgfMap = azrgf.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal));
        List<EcoCompany> companyList = ecoCompanyMapper.selectList(Wrappers.<EcoCompany>lambdaQuery()
                .eq(EcoCompany::getType,"T")
                .eq(EcoCompany::getStatus,"0")
        );
        if (CollUtil.isEmpty(companyList)) {
            throw new ServiceException("无统计单位!");
        }

        List<String> companyNames = companyList.stream().map(EcoCompany::getCompanyName).distinct().sorted().collect(Collectors.toList());

        for (String companyName : companyNames) {
            JtczRcf rgf = new JtczRcf();
            rgf.setDeptName(companyName);
            rgf.setYearTjWccz(tjwcczMap.getOrDefault(companyName, BigDecimal.ZERO));
            rgf.setTjxmrgf(tjrgfMap.getOrDefault(companyName, BigDecimal.ZERO));
            rgf.setYearAzWccz(azwcczMap.getOrDefault(companyName, BigDecimal.ZERO));
            rgf.setAzxmrgf(azrgfMap.getOrDefault(companyName, BigDecimal.ZERO));
            rgf.setCzhj(NumberUtil.add(rgf.getYearTjWccz(),rgf.getYearAzWccz()));
            rgf.setRgfhj(NumberUtil.add(rgf.getTjxmrgf(),rgf.getAzxmrgf()));
            resultList.add(rgf);
        }
        calcZb(resultList);
        calcPm(resultList);
        JtczRcf rgf = new JtczRcf();
        rgf.setDeptName("土建单位小计");
        calcXj(resultList,rgf);
        resultList.add(rgf);
        return resultList;
    }

    private void calcXj(List<JtczRcf> resultList, JtczRcf rgf) {
        BigDecimal tjwccz = BigDecimal.ZERO;
        BigDecimal azwccz = BigDecimal.ZERO;
        BigDecimal tjrgf  = BigDecimal.ZERO;
        BigDecimal azrgf  = BigDecimal.ZERO;
        BigDecimal czhj   = BigDecimal.ZERO;
        BigDecimal rgfhj  = BigDecimal.ZERO;
        for (JtczRcf jtczRcf : resultList) {
            tjwccz = NumberUtil.add(jtczRcf.getYearTjWccz(),tjwccz);
            azwccz = NumberUtil.add(jtczRcf.getYearAzWccz(),azwccz);
            tjrgf  = NumberUtil.add(jtczRcf.getTjxmrgf(),tjrgf );
            azrgf  = NumberUtil.add(jtczRcf.getAzxmrgf(),azrgf );
            czhj   = NumberUtil.add(jtczRcf.getCzhj(),czhj  );
            rgfhj  = NumberUtil.add(jtczRcf.getRgfhj(),rgfhj );
        }
        rgf.setYearTjWccz(tjwccz);
        rgf.setYearAzWccz(azwccz);
        rgf.setTjxmrgf(tjrgf);
        rgf.setAzxmrgf(azrgf);
        rgf.setCzhj(czhj);
        rgf.setRgfhj(rgfhj);

        ArrayList<JtczRcf> tempList = new ArrayList<>();
        tempList.add(rgf);
        calcZb(tempList);
    }

    private void calcPm(List<JtczRcf> resultList) {
        resultList.sort(Comparator.comparing(JtczRcf::getTjxmrgfzb,Comparator.reverseOrder()));
        for (int i = 0; i < resultList.size(); i++) {
            JtczRcf jtczRcf = resultList.get(i);
            jtczRcf.setTjxmrgfpm(i+1);
        }
        resultList.sort(Comparator.comparing(JtczRcf::getAzxmrgfzb,Comparator.reverseOrder()));
        for (int i = 0; i < resultList.size(); i++) {
            JtczRcf jtczRcf = resultList.get(i);
            jtczRcf.setAzxmrgfpm(i+1);
        }
        resultList.sort(Comparator.comparing(JtczRcf::getZb,Comparator.reverseOrder()));
        for (int i = 0; i < resultList.size(); i++) {
            JtczRcf jtczRcf = resultList.get(i);
            jtczRcf.setPm(i+1);
        }
    }

    private void calcZb(List<JtczRcf> resultList) {
        for (JtczRcf jtczRcf : resultList) {
            BigDecimal yearTjWccz = jtczRcf.getYearTjWccz();
            BigDecimal tjxmrgf = jtczRcf.getTjxmrgf();
            BigDecimal yearAzWccz = jtczRcf.getYearAzWccz();
            BigDecimal azxmrgf = jtczRcf.getAzxmrgf();
            BigDecimal czhj = jtczRcf.getCzhj();
            BigDecimal rgfhj = jtczRcf.getRgfhj();
            BigDecimal tjrgfzb = NumberUtil.divideUseProportion(tjxmrgf, 2, yearTjWccz);
            BigDecimal azrgfzb = NumberUtil.divideUseProportion(azxmrgf, 2, yearAzWccz);
            BigDecimal xjZb = NumberUtil.divideUseProportion(rgfhj, 2, czhj);
            jtczRcf.setTjxmrgfzb(tjrgfzb);
            jtczRcf.setAzxmrgfzb(azrgfzb);
            jtczRcf.setZb(xjZb);
        }
    }

}
